//
//  XPVisitorView.m
//  SinaWeibo
//
//  Created by 徐攀 on 16/5/11.
//  Copyright © 2016年 徐攀. All rights reserved.
//

#import "XPVisitorView.h"

@interface XPVisitorView ()

@property (nonatomic, strong) UIImageView *circleImgView;
@property (nonatomic, strong) UIImageView *maskImgView;
@property (nonatomic, strong) UIImageView *iconImgView;
@property (nonatomic, strong) UILabel *remindLabel;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *loginButton;

@end

@implementation XPVisitorView

#pragma mark - 懒加载

- (UIImageView *)circleImgView {
    if (!_circleImgView) {
        _circleImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    }
    return _circleImgView;
}

- (UIImageView *)maskImgView {
    if (!_maskImgView) {
        _maskImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    }
    return _maskImgView;
}

- (UIImageView *)iconImgView {
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    }
    return _iconImgView;
}

- (UILabel *)remindLabel {
    if (!_remindLabel) {
        _remindLabel = [[UILabel alloc] init];
        _remindLabel.text = @"关注一些人，回这里看看有什么惊喜";
        _remindLabel.numberOfLines = 2;
        _remindLabel.textAlignment = NSTextAlignmentCenter;
        _remindLabel.textColor = [UIColor darkGrayColor];
        _remindLabel.font = [UIFont systemFontOfSize:14];
    }
    return _remindLabel;
}

- (UIButton *)registerButton {
    if (!_registerButton) {
        _registerButton =  [self buttonWithTitle:@"注册" backgroudImage:@"common_button_white_disable"];
    }
    return _registerButton;
}

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [self buttonWithTitle:@"登录" backgroudImage:@"common_button_white_disable"];
    }
    return _loginButton;
}

- (UIButton *)buttonWithTitle:(NSString *)title backgroudImage:(NSString *)imgName {
    return [UIButton baseButton:title withFontSize:16 backgroundImage:imgName target:self action:@selector(loginBtnClick)];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.circleImgView];
        [self addSubview:self.maskImgView];
        [self addSubview:self.iconImgView];
        [self addSubview:self.remindLabel];
        [self addSubview:self.registerButton];
        [self addSubview:self.loginButton];
        
    }
    return self;
}

- (void)visitorInfo:(NSString *)text iconImage:(NSString *)imageName{
    if (text && imageName) {
        self.circleImgView.hidden = YES;
        self.remindLabel.text = text;
        self.iconImgView.image = [UIImage imageNamed:imageName];
    }else{
        self.circleImgView.hidden = NO;
        [self startAnimation];
    }
}

- (void)startAnimation {
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotationAnimation.toValue = @(2 * M_PI);
    rotationAnimation.duration = 20;
    rotationAnimation.repeatCount = MAXFLOAT;
    rotationAnimation.removedOnCompletion = NO;
    [self.circleImgView.layer addAnimation:rotationAnimation forKey:nil];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.circleImgView.center = self.center;
    self.maskImgView.center = self.center;
    self.iconImgView.center = self.center;
    
    [self.remindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImgView.mas_bottom);
        make.centerX.equalTo(self);
        make.width.equalTo(@240);
        make.height.equalTo(@40);
    }];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.remindLabel.mas_bottom).offset(10);
        make.left.equalTo(self.remindLabel);
        make.width.equalTo(@100); make.height.equalTo(@35);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.remindLabel.mas_bottom).offset(10);
        make.trailing.equalTo(self.remindLabel);
        make.width.equalTo(@100); make.height.equalTo(@35);
    }];
}

- (void)loginBtnClick {
    if (self.loginBlock ) {
        self.loginBlock();
    }
}

@end
